package com.mark.user.controller;

import com.mark.basic.dto.EmailDto;
import com.mark.basic.dto.PhoneDto;
import com.mark.basic.dto.WeChatDto;
import com.mark.basic.util.BusinessException;
import com.mark.user.service.IUserService;
import com.mark.user.domain.User;
import com.mark.user.query.UserQuery;
import com.mark.basic.util.AjaxResult;
import com.mark.basic.util.PageList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    public IUserService userService;

    /**
     *第三方登录，绑定用户信息

     */
    @PostMapping("/register/wechat")
    public AjaxResult registerWechat(@RequestBody WeChatDto weChatDto){
        try {
            System.out.println(weChatDto);
            Map<String, Object> map = userService.sendregisterWechat(weChatDto);
            return AjaxResult.me().setResultObj(map);
        } catch (BusinessException e) {// 自定义业务异常
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMassage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMassage("系统繁忙");
        }
    }
    /**
     *  邮箱注册
     * @return
     */
    @PostMapping("/register/email")
    public AjaxResult registerEmail(@RequestBody EmailDto emailDto){
        try {
            userService.sendRegisterEmail(emailDto);
            return AjaxResult.me();
        } catch (BusinessException e) {// 自定义业务异常
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMassage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMassage("系统繁忙");
        }
    }

    /**
     *  手机注册
     * @return
     */
    @PostMapping("/register/phone")
    public AjaxResult registerPhone(@RequestBody PhoneDto smsDto){
        try {
            userService.sendRegisterPhone(smsDto);
            return AjaxResult.me();
        } catch (BusinessException e) {// 自定义业务异常
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMassage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMassage("系统繁忙");
        }
    }


    /**
     * 保存和修改公用的
     * @param user  传递的实体
     * @return Ajaxresult转换结果
     */
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody User user){
        try {
            if( user.getId()!=null)
                userService.update(user);
            else
                userService.add(user);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMassage("系统繁忙,请稍后操作！！！").setSuccess(false);

        }
    }
    /**
    * 删除对象信息
    * @param id
    * @return
    */
    @DeleteMapping(value="/{id}")
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            userService.del(id);
            return AjaxResult.me();
        } catch (Exception e) {
        e.printStackTrace();
            return AjaxResult.me().setMassage("系统繁忙,请稍后操作！！！").setSuccess(false);
        }
    }
	
    //获取用户
    @GetMapping("/{id}")
    public User get(@PathVariable("id")Long id)
    {
        return userService.listById(id);
    }


    /**
    * 查看所有的员工信息
    * @return
    */
    @GetMapping()
    public List<User> list(){

        return userService.listAll();
    }


    /**
    * 分页查询数据
    *
    * @param query 查询对象
    * @return PageList 分页对象
    */
    @PostMapping("/list")
    public PageList<User> json(@RequestBody UserQuery query)
    {
        return userService.queryPage(query);
    }
}
